<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>声明式衍生数据 - Vue 3 computed</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }
        #app {
            text-align: center;
            padding: 40px;
            background-color: white;
            border-radius: 12px;
            box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #2c3e50;
            margin-bottom: 30px;
            font-size: 28px;
        }
        p {
            color: #666;
            font-size: 18px;
            margin-bottom: 20px;
            line-height: 1.6;
        }
        h4 {
            color: #e74c3c;
            font-size: 24px;
            margin-bottom: 30px;
            font-weight: bold;
        }
        button {
            padding: 12px 28px;
            font-size: 18px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold;
        }
        button:hover {
            background-color: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(52, 152, 219, 0.3);
        }
        .highlight {
            background-color: #fffacd;
            padding: 20px;
            border-radius: 8px;
            border-left: 4px solid #ffd700;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div id="app">
        <h1>声明式衍生数据 - 计算属性</h1>
        <div class="highlight">
            <p>单价：{{ price }} × 数量：{{ quantity }} × 折扣：{{ discount }}</p>
            <h4>应付：{{ total.toFixed(2) }} 元</h4>
        </div>
        <button @click="add">再来一杯</button>
    </div>

    <script>
        // 创建Vue应用
        const app = Vue.createApp({
            setup() {
                // 创建响应式数据
                const price = Vue.ref(25);
                const quantity = Vue.ref(2);
                const discount = Vue.ref(0.8);

                // 计算购物车总价（自动缓存，只在依赖变化时重新计算）
                const total = Vue.computed(() => {
                    return price.value * quantity.value * discount.value;
                });

                // 再来一杯函数
                const add = () => quantity.value++;

                // 返回模板中使用的数据和方法
                return {
                    price,
                    quantity,
                    discount,
                    total,
                    add
                };
            }
        });

        // 挂载应用
        app.mount('#app');
    </script>
</body>
</html>